package com.xtinno.dal.service;

import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtinno.dal.mapper.SysCaseCauseMapper;
import com.xtinno.dal.po.SysCaseCausePO;
import org.apache.logging.log4j.util.Strings;

import org.springframework.stereotype.Service;

@Service
public class SysCaseCauseService extends ServiceImpl<SysCaseCauseMapper, SysCaseCausePO> {
	/**
	 * 根据 案由名称 和 all parent id 的前缀进行案由的查询.
	 * @param causeName 案由名称.
	 * @param allParentId all parent id 前缀.
	 * @return 匹配的唯一案由.
	 */
	public Optional<SysCaseCausePO> findByNameAndAllParentIdPrefix(String causeName, String allParentId) {
		LambdaQueryWrapper<SysCaseCausePO> wrapper = new LambdaQueryWrapper<>();
		// 校验一下输入的参数，全是空就拉倒.
		if (Strings.isBlank(causeName) && Strings.isBlank(allParentId)) {
			return Optional.empty();
		}

		// 案由
		if (Strings.isNotBlank(causeName)) {
			wrapper.eq(SysCaseCausePO::getCc_name, causeName);
		}

		// all parent id
		if (Strings.isNotBlank(allParentId)) {
			wrapper.likeRight(SysCaseCausePO::getAll_parent_id, allParentId);
		}

		return this.getOneOpt(wrapper, false);
	}
}